import { TForm, TModalResult } from "../hcl/Forms.js";
import { TCombobox, TLable, TCheckBox, TButton, TNumberEdit, TValueMask } from "../hcl/StdCtrls.js";
import { HC, TPaperKind, TPaperOrientation } from "./HCCommon.js";

export class TFrmPageSet extends TForm {
    constructor() {
        super(410, 340);
        this.caption = "页面设置";

        let lbl1 = new TLable("纸张");
        lbl1.left = 23;
        lbl1.top = 21;
        lbl1.width = 30;
        this.addControl(lbl1);

        this.cbbPaper = new TCombobox("");
        this.cbbPaper.left = 53;
        this.cbbPaper.top = 16;
        this.cbbPaper.width = 100;
        this.cbbPaper.static = true;
        this.cbbPaper.onSelectedIndexChange = () => {
            let vPaperSize = HC.GetPaperSizeByName(this.cbbPaper.text);
            if (vPaperSize == TPaperKind.Custom) {
                this.edtWidth.readOnly = false;
                this.edtHeight.readOnly = false;
            } else {
                let vInfo = HC.GetPaperSizeInfo(vPaperSize);
                this.edtWidth.text = vInfo.Width.toString();
                this.edtHeight.text = vInfo.Height.toString();
                this.edtWidth.readOnly = true;
                this.edtHeight.readOnly = true;
            }
        }
        
        this.cbbPaper.addItem("自定义");
        this.cbbPaper.addItem("A3");
        this.cbbPaper.addItem("A4");
        this.cbbPaper.addItem("A5");
        this.cbbPaper.addItem("B5");
        this.cbbPaper.addItem("16K");
        this.cbbPaper.itemIndex = -1;
        this.addControl(this.cbbPaper);

        let lbl2 = new TLable("宽(毫米)");
        lbl2.left = 170;
        lbl2.top = 21;
        lbl2.width = 52;
        this.addControl(lbl2);
        
        this.edtWidth = new TNumberEdit();
        this.edtWidth.valueMask = TValueMask.Integer;
        this.edtWidth.left = 224;
        this.edtWidth.top = 16;
        this.edtWidth.width = 50;
        this.addControl(this.edtWidth);

        let lbl3 = new TLable("高(毫米)");
        lbl3.left = 287;
        lbl3.top = 21;
        lbl3.width = 52;
        this.addControl(lbl3);

        this.edtHeight = new TNumberEdit();
        this.edtHeight.valueMask = TValueMask.Integer;
        this.edtHeight.left = 340;
        this.edtHeight.top = 16;
        this.edtHeight.width = 50;
        this.addControl(this.edtHeight);

        let lbl4 = new TLable("边距(毫米)");
        lbl4.left = 23;
        lbl4.top = 48;
        lbl4.width = 72;
        this.addControl(lbl4);

        this.chkSymmetryMargin = new TCheckBox("对称边距显示");
        this.chkSymmetryMargin.left = 96;
        this.chkSymmetryMargin.top = 47;
        this.chkSymmetryMargin.width = 97;
        this.addControl(this.chkSymmetryMargin);

        let lbl5 = new TLable("上(毫米)");
        lbl5.left = 43;
        lbl5.top = 79;
        lbl5.width = 52;
        this.addControl(lbl5);

        this.edtTop = new TNumberEdit();
        this.edtTop.valueMask = TValueMask.Integer;
        this.edtTop.left = 100;
        this.edtTop.top = 76;
        this.edtTop.width = 50;
        this.addControl(this.edtTop);       

        let lbl6 = new TLable("下(毫米)");
        lbl6.left = 170;
        lbl6.top = 79;
        lbl6.width = 52;
        this.addControl(lbl6);

        this.edtBottom = new TNumberEdit();
        this.edtBottom.valueMask = TValueMask.Integer;
        this.edtBottom.left = 227;
        this.edtBottom.top = 76;
        this.edtBottom.width = 50;
        this.addControl(this.edtBottom);

        let lbl7 = new TLable("左(毫米)");
        lbl7.left = 43;
        lbl7.top = 109;
        lbl7.width = 52;
        this.addControl(lbl7);

        this.edtLeft = new TNumberEdit();
        this.edtLeft.valueMask = TValueMask.Integer;
        this.edtLeft.left = 100;
        this.edtLeft.top = 106;
        this.edtLeft.width = 50;
        this.addControl(this.edtLeft);

        let lbl8 = new TLable("右(毫米)");
        lbl8.left = 170;
        lbl8.top = 109;
        lbl8.width = 52;
        this.addControl(lbl8);

        this.edtRight = new TNumberEdit();
        this.edtRight.valueMask = TValueMask.Integer;
        this.edtRight.left = 227;
        this.edtRight.top = 106;
        this.edtRight.width = 50;
        this.addControl(this.edtRight);

        let lbl9 = new TLable("方向");
        lbl9.left = 23;
        lbl9.top = 146;
        lbl9.width = 30;
        this.addControl(lbl9);

        this.cbbPaperOrientation = new TCombobox("");
        this.cbbPaperOrientation.left = 53;
        this.cbbPaperOrientation.top = 143;
        this.cbbPaperOrientation.width = 100;
        this.cbbPaperOrientation.static = true;
        this.cbbPaperOrientation.addItem("纵向");
        this.cbbPaperOrientation.addItem("横向");
        this.cbbPaperOrientation.itemIndex = 0;
        this.addControl(this.cbbPaperOrientation);

        this.chkPageNoVisible = new TCheckBox("显示页码");
        this.chkPageNoVisible.left = 23;
        this.chkPageNoVisible.top = 183;
        this.addControl(this.chkPageNoVisible);

        this.chkShowLineNo = new TCheckBox("显示行号");
        this.chkShowLineNo.left = 23;
        this.chkShowLineNo.top = 210;
        this.addControl(this.chkShowLineNo);

        this.chkShowUnderLine = new TCheckBox("显示下划线");
        this.chkShowUnderLine.left = 23;
        this.chkShowUnderLine.top = 238;
        this.addControl(this.chkShowUnderLine);

        // let lbl10 = new TLable("页码格式");
        // lbl10.left = 125;
        // lbl10.top = 186;
        // lbl10.width = 52;
        // this.addControl(lbl10);

        // this.cbbPageNoFmt = new TCombobox("");
        // this.cbbPageNoFmt.left = 180;
        // this.cbbPageNoFmt.top = 183;
        // this.cbbPageNoFmt.addItem("{0}/{1}");
        // this.cbbPageNoFmt.addItem("{0}");
        // this.cbbPageNoFmt.addItem("第{0}页/共{1}页");
        // this.cbbPageNoFmt.addItem("第{0}页");
        // this.addControl(this.cbbPageNoFmt);

        this.chkParaLastMark = new TCheckBox("显示换行符");
        this.chkParaLastMark.left = 125;
        this.chkParaLastMark.top = 210;
        this.addControl(this.chkParaLastMark);

        this.chkShowLineActiveMark = new TCheckBox("显示当前编辑行指示符");
        this.chkShowLineActiveMark.left = 125;
        this.chkShowLineActiveMark.top = 238;
        this.addControl(this.chkShowLineActiveMark);

        this.btnOk = new TButton("确定");
        this.btnOk.onClick = () => {
            this.modalResult = TModalResult.Ok;
            this.close();
        }
        this.btnOk.left = Math.trunc((this.width - this.btnOk.width) / 2);
        this.btnOk.top = this.clientArea.height -30;
        this.addControl(this.btnOk);
    }

    SetView(hcView) {
        this.beginUpdate();
        try {
            this.cbbPaper.itemIndex = this.cbbPaper.itemIndexOf(HC.GetPaperSizeName(hcView.ActiveSection.PaperSize));
            if (this.cbbPaper.itemIndex < 0)
                this.cbbPaper.itemIndex = 0;

            //this.edtWidth.readOnly = this.cbbPaper.itemIndex > 0;
            //this.edtHeight.readOnly = this.cbbPaper.itemIndex > 0;

            if (hcView.ActiveSection.PaperOrientation == TPaperOrientation.Portrait) {
                this.cbbPaperOrientation.itemIndex = 0;
                this.edtWidth.text = String.formatFloat(1, hcView.ActiveSection.PaperWidth);
                this.edtHeight.text = String.formatFloat(1, hcView.ActiveSection.PaperHeight);
            } else {
                this.cbbPaperOrientation.itemIndex = 1;
                this.edtWidth.text = String.formatFloat(1, hcView.ActiveSection.PaperHeight);
                this.edtHeight.text = String.formatFloat(1, hcView.ActiveSection.PaperWidth);
            }
        
            this.edtTop.text = String.formatFloat(1, hcView.ActiveSection.PaperMarginTop);
            this.edtLeft.text = String.formatFloat(1, hcView.ActiveSection.PaperMarginLeft);
            this.edtRight.text = String.formatFloat(1, hcView.ActiveSection.PaperMarginRight);
            this.edtBottom.text = String.formatFloat(1, hcView.ActiveSection.PaperMarginBottom);
            //this.cbbPageNoFmt.text = hcView.PageNoFormat;
            this.chkSymmetryMargin.checked = hcView.ActiveSection.SymmetryMargin;
            this.chkPageNoVisible.checked = hcView.ActiveSection.PageNoVisible;
            this.chkParaLastMark.checked = hcView.Style.ShowParaLastMark;
            this.chkShowLineNo.checked = hcView.ShowLineNo;
            this.chkShowLineActiveMark.checked = hcView.ShowLineActiveMark;
            this.chkShowUnderLine.checked = hcView.ShowUnderLine;        

            this.moveCenter();
        } finally {
            this.endUpdate();
        }

        this.showModal(() => {
            hcView.BeginUpdate();
            try {
                let vPaperSize = HC.GetPaperSizeByName(this.cbbPaper.text);
                hcView.ActiveSection.PaperSize = vPaperSize;

                if (this.cbbPaperOrientation.itemIndex == 0)  // 纵向
                    hcView.ActiveSection.PaperOrientation = TPaperOrientation.Portrait
                else
                    hcView.ActiveSection.PaperOrientation = TPaperOrientation.Landscape;

                if (vPaperSize == TPaperKind.Custom) {  // 自定义
                    if (this.cbbPaperOrientation.itemIndex == 0) {  // 纵向
                        hcView.ActiveSection.PaperWidth = parseFloat(this.edtWidth.text);
                        hcView.ActiveSection.PaperHeight = parseFloat(this.edtHeight.text);
                    } else {
                        hcView.ActiveSection.PaperWidth = parseFloat(this.edtHeight.text);
                        hcView.ActiveSection.PaperHeight = parseFloat(this.edtWidth.text);
                    }
                } else {
                    let vPaperInfo = HC.GetPaperSizeInfo(vPaperSize);
                    if (this.cbbPaperOrientation.itemIndex == 0) {  // 纵向
                        hcView.ActiveSection.PaperWidth = vPaperInfo.Width;
                        hcView.ActiveSection.PaperHeight = vPaperInfo.Height;
                    } else {
                        hcView.ActiveSection.PaperWidth = vPaperInfo.Height;
                        hcView.ActiveSection.PaperHeight = vPaperInfo.Width;
                    }
                }

                hcView.ActiveSection.PaperMarginTop = parseFloat(this.edtTop.text);
                hcView.ActiveSection.PaperMarginLeft = parseFloat(this.edtLeft.text);
                hcView.ActiveSection.PaperMarginRight = parseFloat(this.edtRight.text);
                hcView.ActiveSection.PaperMarginBottom = parseFloat(this.edtBottom.text);
                //hcView.PageNoFormat = this.cbbPageNoFmt.text;
                hcView.ActiveSection.SymmetryMargin = this.chkSymmetryMargin.checked;
                hcView.ActiveSection.PageNoVisible = this.chkPageNoVisible.checked;
                hcView.Style.ShowParaLastMark = this.chkParaLastMark.checked;
                hcView.ShowLineNo = this.chkShowLineNo.checked;
                hcView.ShowLineActiveMark = this.chkShowLineActiveMark.checked;
                hcView.ShowUnderLine = this.chkShowUnderLine.checked;
                hcView.ResetActiveSectionMargin();
            } finally {
                hcView.EndUpdate();
            }
        });
    }
}